package com.unknowpupil.colud.consoumer.profile.controller;

import com.unknowpupil.ccommon.utils.GsonUtils;
import com.unknowpupil.cloud.consumer.profile.dto.IconParam;
import com.unknowpupil.cloud.consumer.profile.dto.PasswordParam;
import com.unknowpupil.cloud.consumer.profile.dto.ProfileParam;
import com.unknowpupil.cloud.provider.account.api.IUnAccountService;
import com.unknowpupil.cloud.provider.account.domain.UnAccount;
import com.unknowpupil.cloud.common.dto.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname: ProfileController
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/1/4 13:56
 * @Description:  个人信息的提供者 这是spring cloud 提供者
 * @Version: 1.0
 */
@RestController
@RequestMapping(value = "profile")
@Slf4j
public class ProfileController {
    @Reference(version = "1.0.0")
    private IUnAccountService accountService;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    /**
     * 功能描述: <br>
     * 〈路径参数，符合rest api 风格 feign 是服务与服务之间调用的，这个是前端调用的〉
     *
     * @param: username
     * @since: 1.0.0
     * @auther: w10g
     * @return: UnAccount {@link UnAccount}
     * @Date: 2020/1/4 13:58
     */
    @GetMapping(value = "info/{username}")
    public ResponseResult<UnAccount> info(@PathVariable("username") String username){
        UnAccount account = accountService.get(username);
        log.info("[个人信息] 获取用户信息成功 accunt = {}", GsonUtils.toJson(account));
        return new ResponseResult<>(ResponseResult.CodeStatus.OK,"获取用户信息成功",account);
    }
    /**
     * 功能描述: <br>
     * 〈更新用户信息〉
     *
     * @param: profileParam {@link ProfileParam}
     * @since: 1.0.0
     * @auther: w10g
     * @return:
     * @Date: 2020/1/4 23:21
     */
    @PostMapping(value = "update")
    public ResponseResult<Void> update(@RequestBody ProfileParam profileParam){
        log.info("[个人信息] 更改个人信息 profileParam = ",GsonUtils.toJson(profileParam));
        UnAccount account = new UnAccount();
        //拷贝对象
        BeanUtils.copyProperties(profileParam,account);
        //更新
        int result = accountService.update(account);
        //成功
        if(result>0){
            return new ResponseResult(ResponseResult.CodeStatus.OK,"个人信息更新成功");
        }
        //失败
        else {
            return new ResponseResult(ResponseResult.CodeStatus.FAIL,"个人信息更新失败");
        }
    }

    /**
     * 修改密码
     * @param passwordParam {@link PasswordParam}
     * @return {@link ResponseResult}
     */
   @PostMapping(value = "modify/password")
    public ResponseResult<Void> modifyPassword(@RequestBody PasswordParam passwordParam){
       log.info("[个人信息] 修改密码 passwordParam={}",GsonUtils.toJson(passwordParam));
       UnAccount unAccount = accountService.get(passwordParam.getUsername());
       //旧密码正确
       if(passwordEncoder.matches(passwordParam.getOldPassword(),unAccount.getPassword())){
           int result = accountService.modifyPassword(passwordParam.getUsername(), passwordParam.getPassword());
           if(result>0){
               return new ResponseResult<>(ResponseResult.CodeStatus.OK,"密码更新成功");
           }
       }
       //旧密码错误
       else {
           return new ResponseResult<>(ResponseResult.CodeStatus.FAIL,"密码更新失败");
       }
       return new ResponseResult<>(ResponseResult.CodeStatus.FAIL,"密码更新失败");

   }
    /**
     * 修改头像
     * @param iconParam {@link IconParam}
     * @return {@link ResponseResult}
     */
   @PostMapping(value = "modify/icon")
    public ResponseResult<Void> modifyPassword(@RequestBody IconParam iconParam){
       log.info("[个人信息] 修改头像 iconParam={}",GsonUtils.toJson(iconParam));
       int result = accountService.modifyIcon(iconParam.getUsername(), iconParam.getPath());
       //成功
       if(result>0){
           return new ResponseResult<>(ResponseResult.CodeStatus.OK,"头像更新成功");
       }
       //失败
       else {
           return new ResponseResult<>(ResponseResult.CodeStatus.FAIL,"头像更新失败");
       }
   }


}
